{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "67cd69c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "时间区间\t\t人民币涨幅(CNY)\t上证涨幅\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                             \r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-07-05 ~ 2022-10-31\t8.66%\t\t-15.00%\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                             \r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023-05-09 ~ 2024-02-05\t4.00%\t\t-19.52%\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                             \r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2024-05-08 ~ 2024-09-18\t-1.98%\t\t-13.14%\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                             \r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2024-11-07 ~ 2024-11-26\t1.09%\t\t-6.08%\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                             \r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2024-12-10 ~ 2025-01-13\t1.25%\t\t-7.65%\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                             \r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-03-31 ~ 2025-04-07\t0.79%\t\t-7.17%\n",
      "\n",
      "累计（6个区间）人民币涨幅：13.81%  上证涨幅：-68.56%\n"
     ]
    }
   ],
   "source": [
    "import akshare as ak\n",
    "import pandas as pd\n",
    "\n",
    "# 时间区间列表\n",
    "time_ranges = [\n",
    "    (\"2022-07-05\", \"2022-10-31\"),\n",
    "    (\"2023-05-09\", \"2024-02-05\"),\n",
    "    (\"2024-05-08\", \"2024-09-18\"),\n",
    "    (\"2024-11-07\", \"2024-11-26\"),\n",
    "    (\"2024-12-10\", \"2025-01-13\"),\n",
    "    (\"2025-03-31\", \"2025-04-07\"),\n",
    "]\n",
    "\n",
    "# 获取人民币汇率（美元兑人民币）涨跌幅\n",
    "def cny_pct_change(start_str, end_str):\n",
    "    start_date_str = pd.to_datetime(start_str).strftime(\"%Y%m%d\")\n",
    "    end_date_str = pd.to_datetime(end_str).strftime(\"%Y%m%d\")\n",
    "    df = ak.currency_boc_sina(symbol=\"美元\", start_date=start_date_str, end_date=end_date_str)\n",
    "    df[\"日期\"] = pd.to_datetime(df[\"日期\"])\n",
    "    df = df.sort_values(\"日期\").reset_index(drop=True)\n",
    "    if len(df) < 2:\n",
    "        raise ValueError(\"汇率数据不足\")\n",
    "    start = df[\"中行钞卖价/汇卖价\"].iloc[0]\n",
    "    end = df[\"中行钞卖价/汇卖价\"].iloc[-1]\n",
    "    return (end - start) / start * 100\n",
    "\n",
    "# 获取上证指数涨跌幅\n",
    "def sh_pct_change(start_str, end_str):\n",
    "    df = ak.stock_zh_index_daily(symbol=\"sh000001\")\n",
    "    df[\"date\"] = pd.to_datetime(df[\"date\"])\n",
    "    sub = df[(df[\"date\"] >= start_str) & (df[\"date\"] <= end_str)].sort_values(\"date\")\n",
    "    if len(sub) < 2:\n",
    "        raise ValueError(\"上证指数数据不足\")\n",
    "    return (sub[\"close\"].iloc[-1] - sub[\"close\"].iloc[0]) / sub[\"close\"].iloc[0] * 100\n",
    "\n",
    "# 输出\n",
    "print(\"时间区间\\t\\t人民币涨幅(CNY)\\t上证涨幅\")\n",
    "total_cny = total_sh = valid = 0\n",
    "\n",
    "for s, e in time_ranges:\n",
    "    try:\n",
    "        cp = cny_pct_change(s, e)\n",
    "        sp = sh_pct_change(s, e)\n",
    "        print(f\"{s} ~ {e}\\t{cp:.2f}%\\t\\t{sp:.2f}%\")\n",
    "        total_cny += cp\n",
    "        total_sh += sp\n",
    "        valid += 1\n",
    "    except Exception as err:\n",
    "        print(f\"{s} ~ {e}\\t数据不足：{err}\")\n",
    "\n",
    "if valid:\n",
    "    print(f\"\\n累计（{valid}个区间）人民币涨幅：{total_cny:.2f}%  上证涨幅：{total_sh:.2f}%\")\n",
    "else:\n",
    "    print(\"无有效区间数据\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
